var app = getApp()

Page({
    data: {
        open_animation:true,        // 显示开场动画
        open_src:'http://file.zhijsc.com/20190329101010gih.gif',
        array: [{
            mode: '',
            text: '',
        }],
        src: '../../img/banner.png',
        data: [],
        page: 1,         // 页码

        userData:"",         // 用户数据 openid token

        loading: false,      // 加载中
        noData: false,       // 没有数据
        noMore: false,       // 没有更多
        hint: false,         // 是否提示

        enterStatus:""       // 商家入驻状态
    },
    // 开场动画图片加载完成
    // imgLoad() {
    //     setTimeout(() => {
    //         wx.showTabBar()
    //         this.setData({
    //             open_animation:false
    //         })
    //     }, 3500)
    // },
    getIndexList(res) {
        var self = this;
        var noData = false;                        // 是否没有数据
        var noMore = false;                        // 是否没有更多
        var page = this.data.page;
        var datas = this.data.data;            // 原数据
        
		if (page == 1) { datas = [] }
        wx.request({
            url: app.url.requestUrl + '/auction/index',
            method: 'POST',
            data: {
                openid: res.data.openid,
                token: res.data.token,
                page: page
            },
            success: function (res) {
                var list = res.data;
                if (list.data || datas.length) {
                    noData = 0;
                } else {
                    noData = 1;
                }
                self.setData({
                    noData: noData
                })
                
                if (!list.data.hasNext) {
                    self.setData({ noMore: true })
                }
                if (list.code == 1) {
                    list.data.data.map(function(item,index){
                        item.score = app.globalData.levelRuleCount(item.score);
                    })

                    self.setData({
                        data: datas.concat(list.data.data),
                        loading: false
                    })
                }

                setTimeout(function () {
                    wx.stopPullDownRefresh()
                    wx.hideLoading()
                }, 300)
            }
        })
    },
    getBanner(res){
        var that = this;
        // banner图
        wx.showLoading({ title: '加载中' })
        wx.request({
            url: app.url.requestUrl + '/index/getBanner',
            method: 'POST',
            data: {
                openid: res.data.openid,
                token: res.data.token,
                type:1
            },
            success: function (resbanner) {
                if (resbanner.data.code == 1){
                    that.setData({
                        src: resbanner.data.data.img
                    })
                }else{
					if (resbanner.data.code == -1 || resbanner.data.code == 404){
						that.loginOverdue();
                    }
                }
                that.getIndexList(res);
            }
        })
    },
    // 获取入驻状态
    getEnterStatus(res){
        var self = this;
        var url = "/user/check_status";
        
        wx.request({
            url: app.url.requestUrl + url,
            method: 'POST',
            data: {
                openid: res.data.openid,
                token: res.data.token,
            },
            success: function (status) {
                if (status.data.code == 1) {
                    self.setData({
                        enterStatus: status.data.data
                    })
                }
            }
        })
    },
    // 登录授权
    getWXUserInfo() {
        var self = this;
        //获取用户信息
        wx.getSetting({
            success: res => {
                if (!res.authSetting['scope.userInfo']) {
                    console.log("未授权");
                    this.login.showModalBox()
                } else {
                    console.log("已授权");
                    //已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                    wx.getStorage({
                        key: 'user_info',
                        fail(){
                            wx.getUserInfo({
                                success(res) {
                                    self.uploadUserInfo(res.userInfo);
                                }
                            })
                        }
                    })
                }
            }
        })
    },
    // 确认授权登录
    __accreditEvent(e) {
        this.uploadUserInfo(e.detail.user_info)
        this.login.hideModalBox()
    },
    // 登录成功上传头像及用户信息
    uploadUserInfo(info) {
        var self = this;
        var url = "/user/save_user_info";
        var name = info.nickName;
        var header_img = info.avatarUrl;

        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        name,
                        header_img
                    },
                    success: function (ress) {
                        var datas = ress.data;
                        if(datas.code == 1){
                            wx.setStorage({
                                key: 'user_info',
                                data: info,
                            })
                        }
                    }
                })
            }
        })
    },
    onLoad: function() {
        this.login = this.selectComponent("#login");

        var that = this;

        this.getWXUserInfo();
        wx.getStorage({
            key: 'user',
            success: function (res) {
                that.setData({ userData: res });
                that.getBanner(res)
                that.getIndexList(res);
                that.getEnterStatus(res)
            },
            fail(){
                wx.login({
                    success: res => {
                        console.log("fail")
                        wx.request({
                            url: app.url.requestUrl + '/login/login',
                            method: 'POST',
                            data: {code: res.code},
                            success: function (data) {
                                if (data.data.code == 1) {
                                    wx.setStorage({
                                        key: 'user',
                                        data: data.data.data
                                    })
                                    that.setData({ userData: data.data });
                                    that.getBanner(data.data)
                                    that.getIndexList(data.data);
                                    that.getEnterStatus(data.data);
                                }
                            }
                        })
                    }
                })
            }
        })
    },
	// 登录过期状态
	loginOverdue(e) {
		var self = this;
		wx.removeStorage({
			key: 'user',
			success: function (res) {
				self.onLoad();
			},
		})
	},
    // 下拉刷新
    onPullDownRefresh() {
        this.setData({
            data: [],
            page: 1
        })
        this.onLoad();
    },
    // 加载更多
    onReachBottom() {
        var page = ++this.data.page;
        var noMore = this.data.noMore;
        var loading = this.data.loading;
        var userData = this.data.userData;

        if (!noMore && !loading) {
            this.setData({ page: page, loading: true })
            wx.showLoading({ title: '加载中' })
            this.getIndexList(userData);
        } else {
            if (!this.data.hint) {
                wx.showToast({
                    image: '../../img/warning.png',
                    title: '暂无更多',
                })
                this.setData({ hint: true })
            }
        }
    },
	// 分享
	onShareAppMessage: function () {

	}
})